<?php
class H_Model_SiteMeta extends Zend_Db_Table_Abstract{
    protected $_primary = array('site_id','key');
    protected $_name = "";
    public function __construct() {
        parent::__construct();
        $dbconfig = $this->_db->getConfig();
        $this->_name = $dbconfig['prefix'].'site_meta';
    }
    public function getSiteMeta($site_id,$key){
        $r = $this->find($site_id,$key)->toArray();
        return $r[0];
    }
    public function getSiteMetaValue($site_id,$key){
        $select = $this->_db->select();
        $select->from($this->_name, array('value'))
                ->where('`key` = ?',$key)
                ->where('site_id = ?',$site_id);       
        return $this->_db->fetchOne($select);
    }
    public function getSiteAllMeta($site_id){   
        $where['site_id = ?'] = $site_id;        
        return $this->fetchAll($where)->toArray();
    }
    public function updateSiteMeta($site_id,$key,$value){       
        if ($this->existsSiteKey($site_id, $key)){
            $where['site_id = ?'] = $site_id;
            $where['`key` = ?'] = $key;
            return $this->update(array(
                'value' => $value
            ), $where);
        }else{
            return $this->insert(array(
                'site' => $site_id,
                'key' => $key,
                'value' => $value
            ));
        }
        //INSERT INTO table (primarykeycol,col1,col2) VALUES (1,2,3) ON DUPLICATE KEY UPDATE col1=0, col2=col2+1
    }
    public function existsSiteKey($site_id,$key){
        return count($this->find($site_id,$key)->toArray())>0;        
    }
}